package de.homelabs.homelabs.controller;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.UrlPathHelper;

import de.homelabs.homelabs.plugin.wunderground.HLWundergroundService;
import de.homelabs.modules.content.domain.HLHTMLPageContent;
import de.homelabs.modules.content.domain.HLPageElement;
import de.homelabs.modules.content.domain.HLPluginPageContent;
import de.homelabs.modules.content.service.HLPageManager;

@Controller
public class FrontController {

	//wunderground
	@Autowired
	HLWundergroundService wService;
	
	
	/**
	 * handles all urls not handles by other controllers
	 */
	@RequestMapping("/**/*.htm")
	public ModelAndView defaultHandler(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		//create model
		Map<String, Object> model = new HashMap<String, Object>();
		
		//extract url path
		UrlPathHelper urlPathHelper = new UrlPathHelper();
		String lookuppath = urlPathHelper.getLookupPathForRequest(request);
		
		HLPageElement page = (HLPageManager.getInstance()).getPageElementByURL(lookuppath);
		if (page != null){
			//page found, output content
			StringBuffer content = new StringBuffer();
			for (Object pContent : page.getContents()){

				if (pContent instanceof HLHTMLPageContent)
					content.append(((HLHTMLPageContent) pContent).getContent());
				else if (pContent instanceof HLPluginPageContent){
					//TODO: implement plugin manager
					//test wunderground
					if (((HLPluginPageContent) pContent).getPluginId().equalsIgnoreCase("wunderground")){
						content.append(wService.getWunderGroundData());
					}
				} else {
					System.out.println("nicht unterstuetzter type: " + pContent);
				}
			}
			model.put("content", content);
		} else {
			model.put("content", "keine Seite gefunden");
		}
		
			
		/*URL url = this.getClass().getResource("/files/test.xml");
		InputStream in = this.getClass().getResourceAsStream("/files/test.xml");
				
		System.out.println(url);
		Scanner scanner = new Scanner(in);
		while ( scanner.hasNextLine() ) 
    		System.out.println( scanner.nextLine() ); 
		
		File f = new File(url.toURI());
		FileOutputStream fous = new FileOutputStream(f, true);
		fous.write(new String("eine weitere Zeile").getBytes());
		
		scanner.close();
		fous.close();
		
		in.close();
		*/
		
		javax.naming.Context ctx = new javax.naming.InitialContext();
		Hashtable<?, ?> env = ctx.getEnvironment();
		
		System.out.println("\n\nApplication Environment:\n");
		
		for (Object a : env.keySet()){
			System.out.println("Key: "+a+", Value: "+env.get(a));
		}
		
		Object cPath = ctx.lookup("java:comp/env/configPath");
		System.out.println("\n\n\n\n\ncPath: "+cPath+"\n\n\n");
		//return view
		return new ModelAndView("pagetemplate.ftl", model);
	}
	
	@RequestMapping("/error.htm")
	public ModelAndView errorHandler(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		//create model
		Map<String, Object> model = new HashMap<String, Object>();
		
		//message
		model.put("content", "Fehler bei der Verarbeitung!");
		
		//return view
		return new ModelAndView("pagetemplate.ftl", model);
	}
}
